package com.vito.act.tool;

import java.io.Serializable;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vito.base.util.SpringApplicationContext;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.dao.OaActApproverMapper;
import com.vito.oa.act.extend.service.OaActExMsgService;
import com.vito.oa.act.model.BusinessApproverModel;


/**
 *流程中 任务创建监听
 */
public class ActivitTaskAssignmentListener implements Serializable, TaskListener {
	/**
	 * 
	 */
	private static Logger log = LoggerFactory.getLogger(ActivitTaskAssignmentListener.class);
	private static final long serialVersionUID = -4505427521268903913L;

	@Override
	public void notify(DelegateTask delegateTask) {
		TaskEntity entity =(TaskEntity) delegateTask;
		
		String startUserId = String.valueOf(entity.getVariable(OaActContants.START_USER));
		String assign = entity.getAssignee();
		String processKey =entity.getExecution().getProcessDefinition().getKey();
		String bussId = entity.getProcessInstance().getBusinessKey();
		if(Validata.notNullOrEmpty(assign)){
			//修改节点审批状态为:审批中
			OaActApproverMapper oaActApproverMapper =SpringApplicationContext.getApplicationContext().getBean(OaActApproverMapper.class);
			BusinessApproverModel upModel = oaActApproverMapper.queryaCurrentApprover(bussId, entity.getTaskDefinitionKey());
			if(upModel != null){
				upModel.setApproveStatus(OaActContants.APPROVE_STATUS_APPROVING);
				oaActApproverMapper.updateApproveStatus(upModel);
			}

			OaActExMsgService actExMsgService = SpringApplicationContext.getApplicationContext()
					.getBean(OaActExMsgService.class);
			actExMsgService.sendActExMsg(startUserId, assign, processKey, bussId);
		}
	}
}



